package com.example.study.controller;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.study.entity.Page;
import com.example.study.entity.Result;
import com.example.study.pojo.Menu;
import com.example.study.pojo.User;
import com.example.study.service.UserService;
import com.example.study.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 喜欢多云的季节
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/study/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/select")
    public Result select(@RequestParam Map<String,Object> params, Page page){
        return userService.select(params,page);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> params){
        return userService.delete(params);
    }
    @PostMapping("/update")
    public Result update(User user){
        return userService.update(user);
    }
    @PostMapping("/insert")
    public Result insert(User user){
        return userService.insert(user);
    }
    @PostMapping("/export")
    public void exportEmployee(HttpServletResponse response){
        userService.export(response);
    }
    @PostMapping("/import")
    public String importExl(@RequestParam("file") MultipartFile file){
        ImportParams importParams = new ImportParams();
        importParams.setNeedVerify(false);
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        try {
            ExcelImportResult<User> result =
                    ExcelImportUtil.importExcelMore(file.getInputStream(),User.class, importParams);
            // 当这里就把excel文件的内容根据@Excel注解转换成了对应的实体对象，后续的入库操作这里就省略了
            List<User> studnetGradeList = result.getList();
            // 入库操作 。。。。
            userService.saveBatch(studnetGradeList);
        } catch (IOException e) {
           e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return "导入成功";
    }
    /**
     * @title getMenu
     * @author 田起立
     * @description 获取用户菜单信息
     * @updateTime 2022/8/19 11:28
     * @return null
     */
    @RequestMapping("getMenu")
    public Result getMenu(@RequestParam String token){
        DecodedJWT tokenInfo = JWTUtil.getTokenInfo(token);
        String account = tokenInfo.getClaim("account").asString();
        List<Menu> menu = userService.getMenu(account);
        return Result.ok(menu);
    }
    @RequestMapping("getMenuTest")
    public Result getMenuTest(@RequestParam String account){
        List<Map<String, Object>> menu = userService.menuTest(account);
        List<Object> list=new ArrayList<>();
        menu.forEach(item->{
            List<Map<String, Object>> collect = menu.stream()
                    .filter(
                            it -> {
//                                boolean equals = it.get("pid").toString().equals(item.get("id").toString());
//                                if(equals){
//                                    list.add(it.get("id"));
//                                }
                                 return it.get("pid").toString().equals(item.get("id").toString());
                            })

                    .collect(Collectors.toList());
            item.put("children",collect);
        });
        Map<String, Object> params = new HashMap<>();
        params.put("menuList",menu);
        params.put("pids",list);
        return Result.ok(params);
    }
}

